package service;

import JDBC.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * @author Gao Shiwei
 * user can add a item into collection
 */
@WebServlet(name = "ItemLike")
public class ItemLike extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
        resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        resp.setHeader("Access-Control-Max-Age", "3600");
        resp.addHeader("Access-Control-Allow-Credentials", "true");
        String ItemID=req.getParameter("ItemID");
        HttpSession session = req.getSession();
        String PhoneNumber=(String)session.getAttribute("PhoneNumber");

        String answer=DBUtils.addItemCollect(PhoneNumber,ItemID);
        PrintWriter out = resp.getWriter();
        String str ="{\"answer\":\""+answer+"\"}";
        out.println(str);
        out.flush();
        out.close();
    }
}
